<?php
include_once APPPATH . '/models/constants/flight_constants.php';
include_once APPPATH . '/models/fly_search/airline_company.php';
include_once APPPATH . '/helpers/fly_search_helper.php';
include_once APPPATH . '/services/airline_service.php';
class Fly_search_result_filter_values  extends CI_Model{

    public $minPriceAmount;
    public $maxPriceAmount;
    public $isNoStopFlightExist = false;
    public $isOneStopFlightExist = false;
    public $isTwoMoreStopFlightExist = false;
    public $existAirlineCompanies;
    public $minTotalTravelTime;
    public $maxTotalTravelTime;
    public $existAirlinrCompanyCodes;
    public $goDepartureTimeMinValue;
    public $goDepartureTimeMaxValue;
    public $returnDepartureTimeMinValue;
    public $returnDepartureTimeMaxValue;
    public $goArrivalTimeMinValue;
    public $goArrivalTimeMaxValue;
    public $returnArrivalTimeMinValue;
    public $returnArrivalTimeMaxValue;

    public function setFilterResultValues($combinedAirPriceSolutions = null) {
         $this->existAirlineCompanies = array();
        if ($combinedAirPriceSolutions != null) {
            foreach ($combinedAirPriceSolutions as $combinedAirPriceSolution) {
                $departure_journeys = $combinedAirPriceSolution->departure_journeys;
                $return_journeys = $combinedAirPriceSolution->return_journeys;
                if (!isset($this->minPriceAmount)) {
                    $this->minPriceAmount = $combinedAirPriceSolution->apprixomate_total_price_amount;
                } else if ($combinedAirPriceSolution->apprixomate_total_price_amount <= $this->minPriceAmount) {
                    $this->minPriceAmount = $combinedAirPriceSolution->apprixomate_total_price_amount;
                }

                if (!isset($this->maxPriceAmount)) {
                    $this->maxPriceAmount = $combinedAirPriceSolution->apprixomate_total_price_amount;
                } else if ($combinedAirPriceSolution->apprixomate_total_price_amount >= $this->maxPriceAmount) {
                    $this->maxPriceAmount = $combinedAirPriceSolution->apprixomate_total_price_amount;
                }
               
                // Burda no-stop ,  1 aktarma , veya 2 ve daha fazla aktarma olup olamadıgını belitliyoz
                if ($this->isNoStopFlightExist == false || $this->isOneStopFlightExist == false || $this->isTwoMoreStopFlightExist == false) {
                    foreach ($departure_journeys as $departure_journey) {
                        $stopCount = $departure_journey->getStopCount();
                        $airlineCompany = $departure_journey->getAirlineCompany();
                        $goDepartureTimeTotalMinute  = Fly_seach_helper::getDepartureTimeAsTotalMinute($departure_journey->getDepartureTime());
                        $goArrivalTimeTotalMinute = Fly_seach_helper::getDepartureTimeAsTotalMinute($departure_journey->getArrivalTime());
                        if ($return_journeys != null) {
                            foreach ($return_journeys as $return_journey) {
                                if ($return_journey->related_journey_id == $departure_journey->related_journey_id) {
                                    if ($return_journey->getStopCount() > $stopCount) {
                                        $stopCount = $return_journey->getStopCount();
                                    }
                                    if($airlineCompany != $return_journey->getAirlineCompany()){
                                        $airlineCompany = Fly_Constant::COMBINATION_AIR_COMPANY;
                                    }
                                    $returnDepartureTimeTotalMinute = Fly_seach_helper::getDepartureTimeAsTotalMinute($return_journey->getDepartureTime());
                                    $returnArrivalTimeTotalMinute = Fly_seach_helper::getDepartureTimeAsTotalMinute($return_journey->getArrivalTime());                                    
                                    if(!isset($this->returnDepartureTimeMinValue) || !isset($this->returnDepartureTimeMaxValue)){
                                        $this->returnDepartureTimeMaxValue = $returnDepartureTimeTotalMinute;
                                        $this->returnDepartureTimeMinValue = $returnDepartureTimeTotalMinute;
                                    } else if($returnDepartureTimeTotalMinute < $this->returnDepartureTimeMinValue){
                                        $this->returnDepartureTimeMinValue = $returnDepartureTimeTotalMinute;
                                    } else if ($returnDepartureTimeTotalMinute > $this->returnDepartureTimeMaxValue) {
                                        $this->returnDepartureTimeMaxValue = $returnDepartureTimeTotalMinute;
                                    }
                                  
                                    if(!isset($this->returnArrivalTimeMinValue) || !isset($this->returnArrvialTimeMaxValue)){
                                        $this->returnArrvialTimeMaxValue = $returnArrivalTimeTotalMinute;
                                        $this->returnArrivalTimeMinValue = $returnArrivalTimeTotalMinute;
                                    } else if($returnArrivalTimeTotalMinute < $this->returnArrivalTimeMinValue){
                                        $this->returnArrivalTimeMinValue = $returnArrivalTimeTotalMinute;
                                    } else if ($returnArrivalTimeTotalMinute > $this->returnArrivalTimeMaxValue) {
                                        $this->returnArrivalTimeMaxValue = $returnArrivalTimeTotalMinute;
                                    }
                                    break;
                                }
                            }
                        }
                        
                        
                        if ($stopCount == 0) {
                            $this->isNoStopFlightExist = TRUE;
                        } else if ($stopCount == 1) {
                            $this->isOneStopFlightExist = TRUE;
                        } else if ($stopCount > 1) {
                            $this->isTwoMoreStopFlightExist = TRUE;
                        }
                        
                        if(!isset($this->goDepartureTimeMaxValue) || !isset($this->goDepartureTimeMinValue)){
                            $this->goDepartureTimeMaxValue = $goDepartureTimeTotalMinute;
                            $this->goDepartureTimeMinValue = $goDepartureTimeTotalMinute;
                        }else if($goDepartureTimeTotalMinute < $this->goDepartureTimeMinValue){
                              $this->goDepartureTimeMinValue = $goDepartureTimeTotalMinute;
                        }else if($goDepartureTimeTotalMinute > $this->goDepartureTimeMaxValue){
                             $this->goDepartureTimeMaxValue = $goDepartureTimeTotalMinute; 
                        }
                        
                        if(!isset($this->goArrivalTimeMaxValue) || ! isset($this->goArrivalTimeMinValue)){
                            $this->goArrivalTimeMaxValue = $goArrivalTimeTotalMinute;
                            $this->goArrivalTimeMinValue = $goArrivalTimeTotalMinute;
                        }else if($goArrivalTimeTotalMinute < $this->goArrivalTimeMinValue){
                            $this->goArrivalTimeMinValue = $goArrivalTimeTotalMinute;
                        }else if($goArrivalTimeTotalMinute > $this->goDepartureTimeMaxValue ){
                            $this->goArrivalTimeMaxValue = $goArrivalTimeTotalMinute;
                        }
                        // sonuçlardaki hava yolu şirketi için
                        if(!isset($this->existAirlineCompanies[$airlineCompany])){
                            $airlineCompanyObject = null;
                            if($airlineCompany == Fly_Constant::COMBINATION_AIR_COMPANY){
                                $airlineCompanyObject = new AirlineCompany();
                                $airlineCompanyObject->code = Fly_Constant::COMBINATION_AIR_COMPANY;
                                $airlineCompanyObject->name = "Kombinasyon";
                            }else{
                            $airlineCompanyObject = AirlineService::getAirlineByIATACode($airlineCompany);
                           
                            }
                             $this->existAirlineCompanies[$airlineCompany] = $airlineCompanyObject;
                        }
                    }
                }
                    
                
                
            }
        }
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
